﻿var popupStatus = 0; // set value

$(document).ready(function () {
    /* event for close the popup */
    screenHeight = getScreenHeight();
    screenWidth = getScreenWidth();

    $(".close").click(function () {
        disablePopup();  // function close pop up
    });
});

// Calculate center of screen position for popup
function setPosition() {
    // position for popup
    var screenHeight = getScreenHeight();
    var screenWidth = getScreenWidth();
    var pageWidth = $('#toPopup').width();
    var pageHeight = $('#toPopup').height();
    var top = Math.abs((screenHeight - parseInt(pageHeight)) / 2);
    var left = Math.abs((screenWidth - parseInt(pageWidth)) / 2);

    $('#toPopup').css("top", top);
    $('#toPopup').css("left", left);
}

/************** start: functions. **************/
function showPopup() {
    lockScroll();

    // bind event when press esc key to close popup
    $(document).bind("keydown", function (e) {
        // Esc key
        if (e.keyCode == 27) {
            $(".close").click();
        }
    });

    setTimeout(function () { // then show popup, deley in .75 second
        loadPopup(); // function show popup
    }, 750); // .75 second

    loading(); // loading
    return false;
}

// Show loading image/css
function loading() {
    $("#backgroundPopup").css("opacity", "0.7"); // css opacity, supports IE7, IE8
    $("#backgroundPopup").fadeIn(0001);
    $(".loader").show();
    // $(".circle").show();
    // $(".circle1").show();
}

// Hide loading image/css
function closeloading() {
    $(".loader").fadeOut('normal');
    // $(".circle").fadeOut('normal');
    // $(".circle1").fadeOut('normal');
}

// Show popup on center screen
function loadPopup() {
    if (popupStatus != 0) { // if value is 0, show popup
        return;
    }

    $("#toPopup").fadeIn(0500); // fadein popup div
    popupStatus = 1; // and set value to 1

    setPosition();
    closeloading(); // fadeout loading
}

// Hide popup
function disablePopup() {
    if (popupStatus == 1) { // if value is 1, close popup
        $("#toPopup").fadeOut("normal");
        $("#backgroundPopup").fadeOut("normal");
        popupStatus = 0;  // and set value to 0
        $("#popup").empty();
        // $(window).unbind("scroll");
        $(document).unbind("keydown");
        unlockScroll();
    }
}

function enablePage() {
    closeloading(); // fadeout loading
    $("#backgroundPopup").fadeOut("normal");
}